<?php
/**
 * Babel user profile
 * 
 * @package BABEL
 * @since   BABEL 0.2.0
 * @version $Id: profile.php 768 2008-05-14 09:49:01Z audis $
 * @copyright Copyright (C) 2008 Knut Haug
 * @author  Knut Haug
 * @license LGPL
 */

$deflang = $_SESSION['user']['profile']['active_language'];
$user_id = $_SESSION['user']['user_id'];
$obj_tpl->setFile("babel_profile", "babel/profile.html");

/* {{{ Getting user info from DB */
$DB_QUERY= "
SELECT
	u.experience_points AS exp
FROM
	babel_users u
WHERE
	u.user_id = ?
";
$userinfo = PDODB::execQueryFetch($DB_QUERY, PDO::FETCH_COLUMN, array($user_id));
/* }}} */

// Updates variables in template
$obj_tpl->setVar(
	array(
		"VAR_USER_FULL_NAME"   => $_SESSION["user"]["profile"]["first_name"] . " " . $_SESSION["user"]["profile"]["last_name"],
		"VAR_USER_XP"          => $userinfo["exp"],
		"VAR_USER_COUNTRY"     => "Unknown",	
		"VAR_USER_AGE"         => "Unknown",
	)
);
 
/* {{{ Fetch the user's skill in his/hers known languages.
 *  And updates the session accordingly.
 */
$DB_QUERY= "
SELECT
	skill AS skill,
	language_id AS lang_id
FROM
	babel_user_skills
WHERE 
	user_id = ?
AND
	status = ?
";
if (!PDODB::runQuery($DB_QUERY, array($user_id, BABEL_USER_SKILL_ENABLED), $stmt)) {
	throw new Exception("Selecting profile error");
}

foreach($stmt as $row) {
	$_SESSION["user"]["profile"]["language"][$row["lang_id"]] = $row["skill"];
}

/* }}} */

/*
 * fetch known languages from the DB
 */

$DB_QUERY= "
SELECT
	l.language_id AS langid,
	l.name        AS langname
FROM
	babel_languages l
ORDER BY
	l.name ASC
";

if (!PDODB::runQuery($DB_QUERY, null, $stmt)) {
	throw new Exception("Selecting profile error");
}
/*
 * Print all languages and check the boxes the user is registered in
 */

foreach($stmt as $row) {
	$obj_tpl->setBlock("babel_profile", "languages", "lang_ref");
	if (isset($_SESSION["user"]["profile"]["language"][$row["langid"]])) {
		$obj_tpl->setVar(array(
			"VAR_LANGUAGE_NAME"  => $row["langname"],
			"VAR_LANGUAGE_JSON"  => addslashes($row["langname"]),
			"LANG_ID"            => $row["langid"],	
			"VAR_USER_SKILL"     => $_SESSION["user"]["profile"]["language"][$row["langid"]],
			"VAR_DEFAULT_CHECKED"=> ($row["langid"]==$deflang ? 'checked="checked"':""),
		));
		$obj_tpl->parse("lang_ref", "languages", TRUE);
	}

	$obj_tpl->setBlock("babel_profile", "language_list", "lang_list_ref");
	$obj_tpl->setVar(array(
		"LANG_ID"             => $row["langid"],
		"VAR_LANGUAGE_JSON"   => addslashes($row["langname"]),
	));
	$obj_tpl->parse("lang_list_ref", "language_list", TRUE);
}

$obj_tpl->pparse("babel_profile", "babel_profile");


